const mysql=require('mysql2/promise'),
log=console.log,
{con}=require('./common.js');

async function comment(userId,id,content){
    //unid
    const sql1 = `SELECT max(unid) as length FROM user.comment`;
    let length = await con.execute(sql1);
    let unid = length[0][0].length + 1;
    //commentid
    const sql2 = `SELECT max(commentId) as commentId FROM user.comment`;
    let commentid =  await con.execute(sql2);
    commentid = commentid[0][0].commentId + 1;
    //查找用户头像
    const sql3 = `select image from user.user where id=${userId};`
    let image = await con.execute(sql3);
    image  = image[0][0].image;
    //插入数据
    console.log(content);
    const sql4 = `insert into user.comment values(${unid},
        ${userId},
        ${id},
        ${commentid},
        '2022-10-22',
        '${content}',
        0,
        '${image}',
        '')`
    await con.execute(sql4);
    const sql5 = `update user.essay set essay.comment=essay.comment+1 where id=${id}`;
    await con.execute(sql5);
}


module.exports={comment}